/*
 * =====================================================================================
 *
 *       Filename:  util_event.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2020年06月10日 14时47分28秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#include "util_event.h"
#include "util_debug.h"

#define PIPE_RD 0
#define PIPE_WR 1

struct util_event *util_event_create()
{
    struct util_event *event = (struct util_event *)calloc(1, sizeof(*event));
    if(NULL == event) {
        DEBUG_ERR(__func__, "calloc");
        return NULL;
    }

    /* 初始化 */
    int ret = pipe(event->pipe);
    if(ret < 0) {
        DEBUG_ERR(__func__, "pipe");
        free(event);
        return NULL;
    }

    return event;
}

void util_event_eat(struct util_event *event)
{
    char c;
    while(read(event->pipe[PIPE_RD], &c, 1) > 0) {
        break;
    }
}

void util_event_feed(struct util_event *event)
{
    write(event->pipe[PIPE_WR], "w", 1);
}


